$(function(){
   banner();
   //产品标签页
    navPro();
    //工具提示
    $('[data-toggle="tooltip"]').tooltip();
});

var banner = function (){
    var getData = function (callback){
        if(window.data){
            callback&&callback(window.data);
        } else {
            $.ajax({
               url:"js/data.json",
               data:null,
               dataType:"json",
               success:function (data){
                   window.data = data;
                   callback&&callback(window.data);
               } 
            });
        }     
    };
    var render = function (){
        getData(function (data){
            var isMobile = $(window).width()<768?true:false;
            var pointsTemplate = template("pointsTemplate",{list:data});
            var imageTemplate = template("imageTemplate",{list:data,isMobile:isMobile});
            $(".carousel-indicators").html(pointsTemplate);
            $(".carousel-inner").html(imageTemplate);
        }) ; 
    };
    $(window).on("resize",function (){
        render(); 
    }).trigger("resize");
    
    var startX = 0;
    var distanceX = 0;
    var isMove = false;
    
    $(".wjs_banner").on("touchstart",function (e){
        startX = e.originalEvent.touches[0].clientX;
    }).on("touchmove",function (e){
        var moveX = e.originalEvent.touches[0].clientX;
        distanceX = moveX-startX;
        isMove = true;
    }).on("touchend",function (e){
        if(isMove&&Math.abs(distanceX)>50){
            if(distanceX<0){
                //左滑   下一张
                $(".carousel").carousel("next");
            }else{
                //右滑    上一张
                $(".carousel").carousel("prev");
            }
        }
    });
};

var navPro = function (){
    var $navTabs = $(".wjs_product .nav-tabs");
    var width = 0;
    $navTabs.find("li").each(function(i,item){
        width += $(this).outerWidth(true);
    });
    $navTabs.width(width);
    $(".nav_pro").on('touchmove',function (e){
        e.preventDefault();
    });
    new IScroll(".nav_pro",{
        scrollX:true,
        scrollY:false,
    });
};